﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using elmah2.core.data;

namespace elmah2.core.encoders
{
  public static class HtmlEncoder
  {
    /// <summary>
    /// Encodes the default HTML representation of an <see cref="Error"/> 
    /// object to a string.
    /// </summary>
    public static string EncodeString(Error error)
    {    
      return string.Format("<p class='errorTitle'>{0}<br />{1}</p><pre>{2}</pre>Logged on {3}<br /><br /><div><p class='table-caption'>Server Variables</p>{4}</div>",
        error.Type, error.Message, error.Detail, error.Time.ToShortDateString(), GetServerVarTable(error));
    }

    private static string GetServerVarTable(Error error)
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("<table><th<tr><td>Variable</td><td>Value</td></tr></th><tbody>");
      foreach (string s in error.ServerVariables.Keys)
      {
        sb.Append("<tr>");
        sb.AppendFormat("<td>{0}</td>", s);
        sb.AppendFormat("<td>{0}</td>", error.ServerVariables[s]);
        sb.Append("</tr>");
      }
      sb.Append("</tbody></table>");
      return sb.ToString();
    }
  }
}
